/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.space.regional.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.space.entity.RoomRegional;
import com.fuyao.cloud.space.regional.service.RoomRegionalService;
import com.fuyao.cloud.space.vo.ExcelImportRegionalVO;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 区域管理
 *
 * @author liyue
 * @date 2021-11-09 15:09:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyroomregional")
@Api(value = "fyroomregional", tags = "区域管理")
public class RoomRegionalController {

    private final RoomRegionalService fyRoomRegionalService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param fyRoomRegional 区域管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_view')")
    public R getFyRoomRegionalPage(Page page, RoomRegional fyRoomRegional) {
        return R.ok(fyRoomRegionalService.page(page, Wrappers.query(fyRoomRegional)));
    }

    /**
     * 通过id查询区域管理
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(fyRoomRegionalService.getById(id));
    }

    /**
     * 新增区域管理
     *
     * @param fyRoomRegional 区域管理
     * @return R
     */
    @ApiOperation(value = "新增区域管理", notes = "新增区域管理")
    @SysLog("新增区域管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_add')")
    public R save(@RequestBody RoomRegional fyRoomRegional) {
        fyRoomRegionalService.save(fyRoomRegional);
        return R.ok(fyRoomRegional);
    }

    /**
     * 修改区域管理
     *
     * @param fyRoomRegional 区域管理
     * @return R
     */
    @ApiOperation(value = "修改区域管理", notes = "修改区域管理")
    @SysLog("修改区域管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_edit')")
    public R updateById(@RequestBody RoomRegional fyRoomRegional) {
        return R.ok(fyRoomRegionalService.updateById(fyRoomRegional));
    }

    /**
     * 通过id删除区域管理
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除区域管理", notes = "通过id删除区域管理")
    @SysLog("通过id删除区域管理")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_del')")
    public R removeById(@PathVariable Long id) {
        return fyRoomRegionalService.batchDelete(Collections.singletonList(id));
    }

    /**
     * 通过id删除区域管理
     *
     * @param ids ids
     * @return R
     */
    @ApiOperation(value = "批量删除区域管理信息", notes = "批量删除区域管理信息")
    @SysLog("批量删除区域管理信息")
    @DeleteMapping("batchDelete")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_del')")
    public R batchDelete(@RequestBody List<Long> ids) {
        return fyRoomRegionalService.batchDelete(ids);
    }

    /**
     * 导出excel 表格
     *
     * @param fyRoomRegional 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_export')")
    public List export(RoomRegional fyRoomRegional) {
        return this.fyRoomRegionalService.listRegional(fyRoomRegional);
    }

    /**
     * 返回权型结构空间信息
     *
     * @return 树集合
     */
    @GetMapping(value = "/tree")
    public R getTree() {
        return R.ok(fyRoomRegionalService.selectTree());
    }

    /**
     * 返回权型结构空间信息
     *
     * @return 树集合
     */
    @GetMapping(value = "/landTree")
    public R getLandTree() {
        return R.ok(fyRoomRegionalService.selectLandTree());
    }

    /*
     * 区域创建方法。
     *
     * @return 创建区域对象
     */
    @GetMapping("/create")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_add')")
    public R create() {
        RoomRegional fyRoomRegional = new RoomRegional();
        fyRoomRegional.setUnid(UUID.fastUUID().toString());
        return R.ok(fyRoomRegional,"返回创建对象");
    }

    /**
     * 获取本地（resources）文件
     *
     * @param fileName 文件名称
     * @param response 本地文件
     */
    @SneakyThrows
    @GetMapping("/local/{fileName}")
    public void localFile(@PathVariable String fileName, HttpServletResponse response) {
        ClassPathResource resource = new ClassPathResource("file/" + fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IoUtil.copy(resource.getInputStream(), response.getOutputStream());
    }

    /**
     * 导入区域信息
     *
     * @param excelVOList   区域列表
     * @param bindingResult 错误信息列表
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_export')")
    public R importRegional(@RequestExcel List<ExcelImportRegionalVO> excelVOList, BindingResult bindingResult) {
        return this.fyRoomRegionalService.importRegional(excelVOList, bindingResult);
    }

    /**
     * 通过id查询区域管理
     *
     * @param code id
     * @return R
     */
    @ApiOperation(value = "通过编码查询", notes = "通过id查询")
    @GetMapping("/code/{code}")
    @PreAuthorize("@pms.hasPermission('regional_fyroomregional_view')")
    public R getById(@PathVariable("code") String code) {
        return R.ok(fyRoomRegionalService.getByCode(code));
    }

}
